package com.neusoft.lohas.modules.ivm.web.business;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.neusoft.lohas.common.config.Global;
import com.neusoft.lohas.common.persistence.Page;
import com.neusoft.lohas.common.utils.StringUtils;
import com.neusoft.lohas.common.web.BaseController;
import com.neusoft.lohas.modules.ivm.entity.business.HangStore;
import com.neusoft.lohas.modules.ivm.service.business.HangStoreService;
/**
 * 商圈商店Controller
 * @author Jackie.Liu
 * @version 2015-01-29
 */
@Controller
@RequestMapping(value = "${adminPath}/ivm/business/hangstore")
public class HangStoreController extends BaseController {

	@Autowired
	private HangStoreService hangStoreService;
	
	@ModelAttribute
	public HangStore get(@RequestParam(required=false) String id) {
		if (StringUtils.isNotBlank(id)){
			return hangStoreService.get(id);
		}else{
			return new HangStore();
		}
	}
	
	@RequiresPermissions("ivm:business:hangstore:view")
	@RequestMapping(value = {"list", ""})
	public String list(HangStore hangStore, HttpServletRequest request, HttpServletResponse response, Model model) {
		hangStore.setMerchant(super.merchant);
        Page<HangStore> page = hangStoreService.find(new Page<HangStore>(request, response), hangStore); 
        model.addAttribute("page", page);
		return "modules/ivm/business/hangStoreList";
	}

	@RequiresPermissions("ivm:business:hangstore:view")
	@RequestMapping(value = "form")
	public String form(HangStore hangStore, Model model) {
		if (StringUtils.isBlank(hangStore.getId())) {
			hangStore.setMerchant(super.merchant);
		}
		List<HangStore> choicedList = hangStoreService.findChoicedList(hangStore);
		model.addAttribute("choicedList", choicedList);
		model.addAttribute("hangStore", hangStore);
		return "modules/ivm/business/hangStoreForm";
	}

	@RequiresPermissions("ivm:business:hangstore:edit")
	@RequestMapping(value = "save")
	public String save(HangStore hangStore, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, hangStore)){
			return form(hangStore, model);
		}
		hangStoreService.save(hangStore);
		addMessage(redirectAttributes, "保存商店成功");
		return "redirect:"+Global.getAdminPath()+"/ivm/business/hangstore/?repage";
	}
	
	@RequiresPermissions("ivm:business:hangstore:edit")
	@RequestMapping(value = "delete")
	public String delete(String id, RedirectAttributes redirectAttributes) {
		hangStoreService.delete(id);
		addMessage(redirectAttributes, "删除商店成功");
		return "redirect:"+Global.getAdminPath()+"/ivm/business/hangstore/?repage";
	}

	/**
	 * 批量修改商店排序
	 */
	@RequiresPermissions("ivm:business:hangstore:edit")
	@RequestMapping(value = "updateSort")
	public String updateSort(String[] ids, Integer[] sorts, RedirectAttributes redirectAttributes) {
		if(Global.isDemoMode()){
			addMessage(redirectAttributes, "演示模式，不允许操作！");
			return "redirect:"+Global.getAdminPath()+"/ivm/business/hangstore/";
		}
    	int len = ids.length;
    	HangStore[] stores = new HangStore[len];
    	for (int i = 0; i < len; i++) {
    		stores[i] = hangStoreService.get(ids[i]);
    		stores[i].setSort(sorts[i]);
    		hangStoreService.save(stores[i]);
    	}
    	addMessage(redirectAttributes, "保存商店排序成功!");
		return "redirect:"+Global.getAdminPath()+"/ivm/business/hangstore/";
	}
	
}
